﻿using System;
using System.Reflection;
using WindowsLive.Writer.Api;

namespace SJM.DeliciousBookmarks
{
    static class Utility
    {
        /// <summary>
        /// Convert from IProperties to Options.
        /// </summary>
        /// <param name="properties">The properties.</param>
        /// <returns>An object that starts with default values, and then updates values based on what's in IProperties.</returns>
        public static Options ReadOptionsFromStorage(IProperties properties)
        {
            Options options = new Options();

            foreach (PropertyInfo p in typeof(Options).GetProperties())
            {
                if (p.PropertyType == typeof(int))
                {
                    int defaultValue = (int)p.GetValue(options, null);
                    int newValue = properties.GetInt(p.Name, defaultValue);
                    p.SetValue(options, newValue, null);
                }

                if (p.PropertyType == typeof(bool))
                {
                    bool defaultValue = (bool)p.GetValue(options, null);
                    bool newValue = properties.GetBoolean(p.Name, defaultValue);
                    p.SetValue(options, newValue, null);
                }

                if (p.PropertyType == typeof(string))
                {
                    string defaultValue = (string)p.GetValue(options, null);
                    string newValue = properties.GetString(p.Name, defaultValue);
                    p.SetValue(options, newValue, null);
                }

                if (p.PropertyType == typeof(DateTime))
                {
                    DateTime defaultValue = (DateTime)p.GetValue(options, null);
                    string newValue = properties.GetString(p.Name, defaultValue.ToShortDateString());
                    p.SetValue(options, DateTime.Parse(newValue), null);
                }
            }

            return options;
        }


        /// <summary>
        /// Convert save Options back to IProperties.
        /// </summary>
        /// <param name="properties">The properties.</param>
        /// <param name="options">The options.</param>
        public static void WriteOptionsToStorage(IProperties properties, Options options)
        {
            foreach (PropertyInfo p in typeof(Options).GetProperties())
            {
                if (p.PropertyType == typeof(int?))
                {
                    int value = (int)p.GetValue(options, null);
                    properties.SetInt(p.Name, value);
                }

                if (p.PropertyType == typeof(bool))
                {
                    bool value = (bool)p.GetValue(options, null);
                    properties.SetBoolean(p.Name, value);
                }

                if (p.PropertyType == typeof(string))
                {
                    string value = (string)p.GetValue(options, null);
                    properties.SetString(p.Name, value);
                }

                if (p.PropertyType == typeof(DateTime))
                {
                    DateTime value = (DateTime)p.GetValue(options, null);
                    properties.SetString(p.Name, value.ToShortDateString());
                }
            }
        }
    }
}
